﻿// License: https://opensource.org/licenses/MIT
// Create Date : 2018-01-01

using System;

namespace Cat.Extensions
{
    internal static partial class StringEx
    {
        /// <summary>
        /// 不会抛异常的Format 主要用于不希望抛异常的极端环境(如日志)
        /// </summary>
        public static String SafeFormat(this String format, object arg0)
        {
            try
            {
                if (format == null) return "<null>";
                if (format.Length == 0) return "<Empty>";
                //if (String.IsNullOrEmpty(format)) return String.Empty;
                return String.Format(format, arg0);
            }
            catch
            {
                return String.Format("\"{0}\".Format(\"{1}\")", format, arg0);
            }
        }
        /// <summary>
        /// 不会抛异常的Format 主要用于不希望抛异常的极端环境(如日志)
        /// </summary>
        public static String SafeFormat(this String format, object arg0, object arg1)
        {
            try
            {
                if (format == null) return "<null>";
                if (format.Length == 0) return "<Empty>";
                //if (String.IsNullOrEmpty(format)) return String.Empty;
                return String.Format(format, arg0, arg1);
            }
            catch
            {
                return String.Format("\"{0}\".Format(\"{1}\",\"{2}\")", format, arg0, arg1);
            }
        }
        /// <summary>
        /// 不会抛异常的Format 主要用于不希望抛异常的极端环境(如日志)
        /// </summary>
        public static String SafeFormat(this String format, object arg0, object arg1, object arg2)
        {
            try
            {
                if (format == null) return "<null>";
                if (format.Length == 0) return "<Empty>";
                //if (String.IsNullOrEmpty(format)) return String.Empty;
                return String.Format(format, arg0, arg1, arg2);
            }
            catch
            {
                return String.Format("\"{0}\".Format(\"{1}\",\"{2}\",\"{3}\")", format, arg0, arg1, arg2);
            }
        }
        /// <summary>
        /// 不会抛异常的Format 主要用于不希望抛异常的极端环境(如日志)
        /// </summary>
        public static String SafeFormat(this String format, params object[] args)
        {
            try
            {
                if (format == null) return "<null>";
                if (format.Length == 0) return "<Empty>";
                //if (String.IsNullOrEmpty(format)) return String.Empty;
                if (args == null || args.Length == 0) return format;
                return String.Format(format, args);
            }
            catch
            {
                return String.Format("\"{0}\".Format(\"{1}\")", format, String.Join("\",\"", args));
            }
        }
    }
}
